package com.qf.springmvc02.controller;

import com.github.pagehelper.PageInfo;
import com.qf.springmvc02.entity.User;
import com.qf.springmvc02.response.Response;
import com.qf.springmvc02.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 钻五大人
 * @date 2022/5/16 19:55
 * 获取用户列表/根据ID查询用户/便捷/删除
 */
@RestController
@RequestMapping("/vue")
public class VueUserController {

    @Autowired
    UserService userService;

    @RequestMapping("/users")
    public Response<PageInfo<User>> getAllUser(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                                               @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageInfo<User> pageInfo = userService.selectAllUser(pageIndex, pageSize);
        return Response.success(pageInfo);
    }

    /**
     * 根据ID删除用户
     *
     * @param id 用户ID
     * @return 响应对象
     */
    @GetMapping("/user/{id}")
    public Response<User> deleteById(@PathVariable("id") Integer id) {
        userService.deleteById(id);
        return Response.success();
    }

    /**
     * 根据ID查询单个用户
     *
     * @param id 用户ID
     * @return 响应对象
     */
    @GetMapping("/getUser/{id}")
    public Response<User> selectById(@PathVariable("id") Integer id) {
        return Response.success(userService.selectById(id));
    }

    /**
     * 编辑用户
     *
     * @param id 用户ID
     * @return 响应对象
     */
    @PostMapping("/user")
    public Response<Integer> updateUser(User user) {
        return Response.success(userService.update(user));
    }
}
